//
//  LocalViewController.m
//  SXHomeTownClub
//
//  Created by 周思扬 on 2018/5/28.
//  Copyright © 2018年 jsx. All rights reserved.
//

#import "LocalViewController.h"
#import "LocalCell.h"
#import "LocalModel.h"

@interface LocalViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *localDataSource;

@end

@implementation LocalViewController

- (void)addSubViews {
    UITableView *localTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    localTableView.tableFooterView = [UIView new];
    localTableView.delegate = self;
    localTableView.dataSource = self;
    [localTableView registerClass:[LocalCell class] forCellReuseIdentifier:@"local"];
    localTableView.estimatedRowHeight = 315;
    localTableView.rowHeight = UITableViewAutomaticDimension;
    [self.view addSubview:localTableView];
    
    self.baseTableview = localTableView;
    [self obtainLocalDataSource];
}

- (void)obtainLocalDataSource {
    [JSXHttpTool Post:@"https://dzapi.powerdadmom.com/index.php?s=api/Quan/localQuan" params:@{@"gcid":@"3", @"page":@"1"} success:^(id json) {
        for (NSDictionary *dict in json) {
            LocalModel *local = [[LocalModel alloc] initWithDictionary:dict];
            [self.localDataSource addObject:local];
        }
        [self.baseTableview reloadData];
    } failure:^(NSError *error) {
        NSLog(@"error %@", error.description);
    }];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.localDataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LocalCell *cell = [tableView dequeueReusableCellWithIdentifier:@"local" forIndexPath:indexPath];
    LocalModel *local = self.localDataSource[indexPath.row];
    cell.localModel = local;
    return cell;
}

#pragma mark - lazy
- (NSMutableArray *)localDataSource {
    if (_localDataSource == nil) {
        _localDataSource = [NSMutableArray array];
    }
    return _localDataSource;
}

@end
